import { Configuration, App } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
import { join } from 'path';
// import { DefaultErrorFilter } from './filter/default.filter';
// import { NotFoundFilter } from './filter/notfound.filter';
import { ReportMiddleware } from './middleware/report.middleware';
import { AuthMiddleware } from './middleware/auth.middleware';

import * as jwt from '@midwayjs/jwt';
import * as cacheManager from '@midwayjs/cache-manager';
import * as orm from '@midwayjs/typeorm';
import * as fs from 'fs';

import * as busboy from '@midwayjs/busboy';

@Configuration({
  imports: [
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ['local']
    },
    orm,
    jwt,
    cacheManager,
    busboy
  ],
  importConfigs: [join(__dirname, './config')]
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application;

  async onReady() {
    // add middleware
    this.app.useMiddleware([ReportMiddleware, AuthMiddleware]);
    // add filter
    // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
    // 应用启动时候，需要判断 工程根目录下是否存在 tmp 文件夹
    // 如果不存在，需要创建
    const tmpDir = join(__dirname, '../tmp');
    if (!fs.existsSync(tmpDir)) {
      console.log('tmpDir not exists, create it');
      fs.mkdirSync(tmpDir);
    }

    console.log('onReady');
  }
}
